package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.domain.Item;
import isp.apps.example.shoppingcart.domain.ShoppingCartMessageFactory;
import isp.apps.example.shoppingcart.persistence.ItemDirectory;
import isp.apps.example.shoppingcart.persistence.factory.ItemPersistenceFactory;
import isp.apps.example.shoppingcart.persistence.ItemRepository;

import isp.apps.messageFramework.SystemMessage;
import isp.apps.messageFramework.retriever.MessageRetriever;
import java.math.BigDecimal;
import java.util.List;

public class NewItemAction extends ActionSupport
{
  private List items;
  private MessageRetriever messageRetriever = ShoppingCartMessageFactory.getMessageRetriever();
  private String newItemName, newItemDescription, newItemSku, newItemCost;

  public NewItemAction()
  {
  }

  public void setItems(List pItems)
  {
    items = pItems;
  }

  public List getItems()
  {
    return items;
  }

  public String execute()
  {
    validate();
    if (hasActionErrors())
      return SUCCESS;
    writeToRepository();
     return SUCCESS;
  }

  public void validate()
  {
    ShoppingCartValidator scv = new ShoppingCartValidator();
    if ((newItemName = scv.checkText(newItemName)) == null)
    {
      addActionError("Please specify a valid name for the new item.");
      return;
    }

    if ((newItemDescription = scv.checkText(newItemDescription)) == null)
    {
      addActionError(messageRetriever.getMessage(new SystemMessage("newitemaction.invalidItemDescription")));
      return;
    }

    if ((newItemCost = scv.checkCurrency(newItemCost)) == null)
    {
      addActionError("Please specify a valid cost for the new item (1-7 digits, optionally 2 decimal places).");
      return;
    }
 
    if ((newItemSku = scv.checkAlphaNumeric(newItemSku)) == null)   
    {
      addActionError("Please specify a valid alphanumeric sku for the new item.");
      return;
    }
    ItemDirectory id = ItemPersistenceFactory.getItemDirectory();
    if (id.getItemBySku(newItemSku) != null)
      addActionError("An item with sku: " + newItemSku + " already exists");
  }

  public void writeToRepository()
  {
    ItemRepository ir = ItemPersistenceFactory.getItemRepository();
    Item i = new Item();
    i.setName(newItemName);
    i.setDescription(newItemDescription);
    i.setCost(new BigDecimal(newItemCost));
    i.setSku(newItemSku);
    ir.saveItem(i);

  }

  public void setNewItemName(String newItemName)
  {
    this.newItemName = newItemName;
  }

  public void setNewItemDescription(String newItemDescription)
  {
    this.newItemDescription = newItemDescription;
  }

  public void setNewItemSku(String newItemSku)
  {
    this.newItemSku = newItemSku;
  }

  public void setNewItemCost(String newItemCost)
  {
    this.newItemCost = newItemCost;
  }

  public String getNewItemName()
  {
    return newItemName;
  }

  public String getNewItemDescription()
  {
    return newItemDescription;
  }

  public String getNewItemSku()
  {
    return newItemSku;
  }

  public String getNewItemCost()
  {
    return newItemCost;
  }
}
